package com.marchsoft.campushelp.rest;

import com.marchsoft.campushelp.domain.vo.PasswordChange;
import com.marchsoft.campushelp.domain.vo.PhoneChange;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.aop.log.Log;
import com.marchsoft.campushelp.domain.CampusUserTable;
import com.marchsoft.campushelp.service.CampusUserTableService;
import com.marchsoft.campushelp.service.dto.CampusUserTableQueryCriteria;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author 李诗莹
* @date 2020-03-13
*/
@Api(tags = "用户表管理")
@RestController
@RequestMapping("/auth/campusUserTable")
public class CampusUserTableController {
    @Autowired
    private PasswordEncoder passwordEncoder;

    private final CampusUserTableService campusUserTableService;

    public CampusUserTableController(CampusUserTableService campusUserTableService) {
        this.campusUserTableService = campusUserTableService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('campusUserTable:list')")
    public void download(HttpServletResponse response, CampusUserTableQueryCriteria criteria) throws IOException {
        campusUserTableService.download(campusUserTableService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询用户表")
    @ApiOperation("查询用户表")
//    @PreAuthorize("@el.check('campusUserTable:list')")
    @AnonymousAccess
    public ResponseEntity<Object> getCampusUserTables(CampusUserTableQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(campusUserTableService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增用户表")
    @ApiOperation("新增用户表")
//    @PreAuthorize("@el.check('campusUserTable:add')")
    @AnonymousAccess
    public ResponseEntity<Object> create(@Validated @RequestBody CampusUserTable resources){
        System.out.println(resources.getLoginPassword());
        String encode = passwordEncoder.encode(resources.getLoginPassword());
        resources.setLoginPassword(encode);
        System.out.println(resources.getLoginPassword());
        return new ResponseEntity<>(campusUserTableService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改用户表")
    @ApiOperation("修改用户表")
    @PreAuthorize("@el.check('campusUserTable:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody CampusUserTable resources){
        campusUserTableService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除用户表")
    @ApiOperation("删除用户表")
    @PreAuthorize("@el.check('campusUserTable:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Long[] ids) {
        campusUserTableService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
    @ApiOperation("修改密码")
    @PostMapping(value = "/updatePass")
//    @AnonymousAccess
    public ResponseEntity<Object> updataPassword(@RequestBody PasswordChange passwordChange){
        UserDetails userDetails = SecurityUtils.getUserDetails();
//        System.out.println(userDetails.getUsername()+"----"+userDetails.getPassword());
        CampusUserTable userByPhone = campusUserTableService.findUserByPhone(userDetails.getUsername());
        if(!passwordEncoder.matches(passwordChange.getOldPassword(), userByPhone.getLoginPassword())){
            throw new BadRequestException("修改失败，旧密码错误");
        }
        if(passwordEncoder.matches(passwordChange.getNewPassword(), userByPhone.getLoginPassword())){
            throw new BadRequestException("新密码不能与旧密码相同");
        }
        campusUserTableService.updatePass(userByPhone.getPhone(),passwordEncoder.encode(passwordChange.getNewPassword()));
        return new ResponseEntity<>(HttpStatus.OK);

    }
    @ApiOperation("修改密码")
    @PostMapping(value = "/updateNickName")
    public ResponseEntity<Object> updataNickName(@RequestBody String nickName){
        UserDetails userDetails = SecurityUtils.getUserDetails();
        CampusUserTable userByPhone = campusUserTableService.findUserByPhone(userDetails.getUsername());
        campusUserTableService.updatenickName(userByPhone.getId(),nickName);
        return new ResponseEntity<>(HttpStatus.OK);
    }
    @ApiOperation("修改密码")
    @PostMapping(value = "/updatePhone")
    public ResponseEntity<Object> updataPhone(@RequestBody String phoneChange){

        UserDetails userDetails = SecurityUtils.getUserDetails();
        CampusUserTable userByPhone = campusUserTableService.findUserByPhone(userDetails.getUsername());
        if(phoneChange.equals(userByPhone.getPhone())){
            throw new BadRequestException("新手机号不能与原手机号相同");
        }

        campusUserTableService.updatePhone(userByPhone.getId(),phoneChange);
        return new ResponseEntity<>(HttpStatus.OK);
    }






//    @RequestMapping("/text")
////    @AnonymousAccess
//    @PreAuthorize("@el.check('4')")
//    public void aa(){
////        boolean matches = passwordEncoder.matches("123456", "$2a$10$zi6NOljzk2DPSdAG5Fy0suuSAyP0/5k8XNSTRkE0xxdQdXDCPAN9a");
//        boolean matches = passwordEncoder.matches("123456", "$2a$10$fP.426qKaTmix50Oln8L.uav55gELhAd0Eg66Av4oG86u8km7D/Ky");
//        System.out.println(matches);
//
//    }
//    @RequestMapping("/text2")
//    @AnonymousAccess
//    public MyJwtUser bb(){
//
//        return (MyJwtUser)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//    }
}